//
//  TopViewController.m
//  HuiWenMovie
//
//  Created by ios on 15/7/19.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "TopViewController.h"
#import "DataService.h"
#import "TopModel.h"
#import "TopCollectionViewCell.h"
#import "CommentViewController.h"
#import "BaseNavigationController.h"
#define kwth [UIScreen mainScreen].bounds.size.width
#define kheight [UIScreen mainScreen].bounds.size.height
@interface TopViewController ()

@end

@implementation TopViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self)
    {
        self.title = @"TOP";
        
    }
    
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    [self configureTopCollectionView];
    
    
    // Do any additional setup after loading the view.
}

- (void)configureTopCollectionView
{

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake((kwth-40)/3,180);
    flowLayout.minimumInteritemSpacing = 3;
    flowLayout.minimumLineSpacing = 10;
    

    
    _topCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    UINib *nib = [UINib nibWithNibName:@"TopCollectionViewCell" bundle:[NSBundle mainBundle]];
    [_topCollectionView registerNib:nib forCellWithReuseIdentifier:@"cell3"];
    _topCollectionView.dataSource = self;
    _topCollectionView.delegate = self;
    
    
    [self.view addSubview:_topCollectionView];
}
- (void)loadData
{
    NSDictionary *dic = [[NSDictionary alloc]init];
    dic = [DataService getJsonDataFromFile:@"top250.json"];
    NSArray *array = [dic valueForKey:@"subjects"];
    _content = [[NSMutableArray alloc]init];
    for (NSDictionary *dic in array) {
        TopModel *topModel = [[TopModel alloc]init];
        topModel.title = [dic valueForKey:@"title"];
        topModel.medium = [[dic valueForKey:@"images"] valueForKey:@"medium"];
        NSNumber *temp = [[dic valueForKey:@"rating"] valueForKey:@"average"];
        topModel.average = [temp floatValue];
        [_content addObject:topModel];
    }
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return [_content count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    
    TopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell3" forIndexPath:indexPath];
   // NSLog(@"%@",_content[indexPath.row]);
    cell.topModel = _content[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CommentViewController *cv = [[CommentViewController alloc] init];
    BaseNavigationController *navForComment = [[BaseNavigationController alloc]initWithRootViewController:cv];
    
    [self presentViewController:navForComment animated:YES completion:nil];
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
